setwd("/Users/hanslueders/Dropbox (IPL)/Data copy/Shape_Files_For_Anna copy")

library(foreign)


files<-c( paste( seq(1100,1295,by=5),"_poly",".dbf",sep="") ,paste("poly_", seq(1300,1790,by=5),".dbf",sep="") ) 

years<-seq(1100,1790,by=5)

data_list<-list()

for(i in 1:length(files)){

data_list[[i]]<-read.dbf(file=files[i])

data_list[[i]]$Year <- years[i]


}


### Puts Covariates in the Same Order in Data Frame ###

data_list2<-list()

for(i in 1:length(data_list)){
  
  
  data_list2[[i]]<-cbind(as.character(data_list[[i]]$Name),data_list[[i]]$Year,as.numeric(as.character(data_list[[i]]$X)),as.numeric(as.character(data_list[[i]]$Y)),as.numeric(as.character(data_list[[i]]$Area)))
  
}


data_q<-as.data.frame(do.call(rbind,data_list2))



names(data_q)<-c("Name","Year","X","Y","Area")

data_q$Area<-as.numeric(as.character(data_q$Area))

data_q$logArea<-log(data_q$Area)


### Fixes Spelling of Names Inconsistencies ####


data_q$Name<-ifelse(data_q$Name =="Balearic islands" ,"Balearic Islands",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Albanians" ,"Albania",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Almavorid_4" ,"Almoravid_4",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Almafi","Amalfi",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Bresancon","Besancon",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Stratsbourg","Strasbourg",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Russians" ,"Russians_1",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Correggio" ,"Corregio" ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Wurrtemberg","Wurttemberg",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Ertfurt" ,"Erfurt" ,as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Regensberg" ,"Regensburg"  ,as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Ullster","Ulster",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="The  Holy Roman Empire","The Holy Roman Empire",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_1","Fatimid_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_2","Fatimid_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_3","Fatimid_3",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_4","Fatimid_4",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_5","Fatimid_5",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Fatimad_6","Fatimid_6",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Kevian Rus","Kievan Rus",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Lithuania","Lithuanians",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Polaban Slavs","Polabian Slavs",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Poblabian Slavs","Polabian Slavs",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Pechengens","Pechenegs",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Kingdom of Burgandy","Kingdom of Burgundy",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Leistner","Leinster",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="Noresmen","Norsemen",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name =="South_Slavs","South Slavs",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Strachclyde","Strathclyde",as.character(data_q$Name))



data_q$Name<-ifelse(data_q$Name == "Umayaa_1_4_1","Ummayad_1_4_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_1","Ummayad_1_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_2","Ummayad_1_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_3","Ummayad_1_3",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_4","Ummayad_1_4",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_4_1","Ummayad_1_4_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_1_4_2","Ummayad_1_4_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_2","Ummayad_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_3","Ummayad_3",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_3_1","Ummayad_3_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_3_2","Ummayad_3_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_4","Ummayad_4",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_4_1","Ummayad_4_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_4_1_1","Ummayad_4_1_1",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Ummayaad_4_1_1","Ummayad_4_1_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_4_1_2","Ummayad_4_1_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_4_2","Ummayad_4_2",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaad_5","Ummayad_5",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayaard_3_1","Ummayad_3_1",as.character(data_q$Name))


data_q$Name<-ifelse(data_q$Name == "Umayyad_3_2","Ummayad_3_2",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Geona" , "Genoa",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Almovorads", "Almoravids",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Balearic" | data_q$Name ==         "Balearic islands"  , "Balearic Islands",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Hammadid", "Hammadids",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Norseman", "Norsemen",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Polostk", "Polotsk",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Munstert", "Munster",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Alhomad" | data_q$Name ==  "Almohad" , "Almohads",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Bresica"  , "Brescia"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Bulgarians"  ,  "Bulgaria"   ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Catile" | data_q$Name == "Castille", "Castile" ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==    "Cremoa"   ,   "Cremona"    ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==    "Dauhphine"   ,    "Dauphine"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Felte and Belluno"  | data_q$Name == "Feltre amd Belluno",  "Feltre and Belluno"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==   "Franche Compte" ,    "Franche Comte" ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==   "Kingdom of the Sword" ,     "Knights of the Sword"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==   "Kingdon of Naples"| data_q$Name == "The Kingdom of Naples" ,  "Kingdom of Naples"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==   "Pistoa"   ,     "Pistoia"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Portgual"     ,    "Portugal"   ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==   "The Republic of Novgorod"      ,   "Republic of Novgorod"   ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==    "Trevisto" ,   "Treviso"   ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Entense","Estense",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Freising","Friesberg",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="GIlan","Gilan",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Granada","Grenada",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name =="Hapsburgs" ,"Habsburgs",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Il Khanid Mongols", "Il-Khanid Mongols",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name == "Knights Hospitaller", "Knights Hopitaller",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Luxembourg", "Luxembourgs",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Mongol Empires", "Mongols",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Mongol Empire", "Mongols",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Vulga Bulgars", "Volga Bulgars"  ,as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Wittelsbachs", "Wittlesbachs",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Wurtemburg", "Wurttemberg",as.character(data_q$Name))

data_q$Name<-ifelse(data_q$Name ==  "Hainaut", "Hainault",as.character(data_q$Name))




data_q<-subset(data_q,!is.na(data_q$Name))

data_q$X<-as.numeric(as.character(data_q$X))

data_q$Y<-as.numeric(as.character(data_q$Y))

data_q$Area<-as.numeric(as.character(data_q$Area))

data_q$logArea<-log(data_q$Area)

data_q<-subset(data_q,data_q$Area > 6)



